package mvc.model

import org.json.JSONObject
import java.text.SimpleDateFormat
import java.util.*

class HeatData(
    val startTime: String, val endTime: String, val maxTempValue: Int,
    val minTempValue: Int, val timeOut: Int
) {
    companion object {

        fun default(): HeatData {
            return HeatData(
                "04:00",
                "23:59",
                0,
                0,
                30
            )
        }

        fun withJson(json: JSONObject): HeatData {
            val startTime = json.getString("startTime")
            val endTime = json.getString("endTime")
            var start = json.getDouble("start").toInt()
            var end = json.getDouble("end").toInt()
            val timeOut = json.getInt("more")
            if (start < 0) {
                start = 0
            }
            if (end < 0) {
                end = 0
            }
            return HeatData(startTime, endTime, start, end, timeOut)
        }
    }

    private val startTimeInt: Int
    private val endTimeInt: Int

    init {
        val format = SimpleDateFormat("HH:mm")
        val date1 = format.parse(startTime)
        startTimeInt = date1.hours * 60 + date1.minutes

        val date2 = format.parse(endTime)
        endTimeInt = date2.hours * 60 + date2.minutes
    }

    fun isExist(): Boolean {
        val calendar = Calendar.getInstance()
        val cc = calendar.get(Calendar.HOUR_OF_DAY) * 60 + calendar.get(Calendar.MINUTE)
        return (cc >= startTimeInt) && (cc <= endTimeInt)
    }

    override fun toString(): String {
        return "HeatData(startTime='$startTime', endTime='$endTime', maxTempValue=$maxTempValue, minTempValue=$minTempValue, timeOut=$timeOut, startTimeInt=$startTimeInt, endTimeInt=$endTimeInt)"
    }

}